package miku.firstgame.ui.component
{

	import com.saia.starlingPunk.SP;

	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.TextInput;
	import feathers.core.FeathersControl;

	import miku.firstgame.event.RoomEvent;
	import miku.firstgame.util.StringUtil;

	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;

	public class CreateRoomPanel extends FeathersControl
	{
		private var back:Quad;

		private var cancelButton:Button;
		private var okButton:Button;
		private var nameInfo:Label;
		private var nameInput:TextInput;

		private var infoLabel:Label;

		public function CreateRoomPanel()
		{
			super();
		}

		override protected function initialize():void
		{
			super.initialize();
			createChildren();

		}

		protected function createChildren():void
		{
			back = new Quad(width, height, 0x24677f);
			addChild(back);

			cancelButton = new Button();
			cancelButton.label = "取消";
			cancelButton.addEventListener(Event.TRIGGERED, onCancelButtonClick);
			addChild(cancelButton);
			cancelButton.validate();

			okButton = new Button();
			okButton.label = "创建";
			okButton.addEventListener(Event.TRIGGERED, onOkButtonClick);
			addChild(okButton);
			okButton.validate()

			nameInfo = new Label();
			nameInfo.text = "请输入要创建的房间名字:";
			addChild(nameInfo);
			nameInfo.validate();

			nameInput = new TextInput();
			addChild(nameInput);
			nameInput.validate();

			infoLabel = new Label();
			addChild(infoLabel);
		}

		override protected function draw():void
		{
			super.draw();
			var sizeInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_SIZE);
			if (sizeInvalid)
			{
				resize();
			}

		}

		public function resize():void
		{
			back.width = this.actualWidth;
			back.height = this.actualHeight;

			okButton.x = (this.width - (okButton.width + cancelButton.width + 20)) >> 1;
			okButton.y = (this.height >> 1) + 30;

			cancelButton.x = okButton.x + okButton.width + 20;
			cancelButton.y = okButton.y;

			nameInput.x = this.width - nameInput.width >> 1;
			nameInput.y = (this.actualHeight >> 1) - 20;

			nameInfo.x = nameInput.x;
			nameInfo.y = nameInput.y - 40;

			infoLabel.x = nameInfo.x;
			infoLabel.y = okButton.y + okButton.height + 10;

			//nameInfo.x = 

		}

		private function onCancelButtonClick(e:Event):void
		{
			var event:RoomEvent = new RoomEvent(RoomEvent.ROOM_CANCEL);
			dispatchEvent(event);
		}

		private function onOkButtonClick(e:Event):void
		{
			var name:String = StringUtil.trim(nameInput.text);
			if (name)
			{
				var event:RoomEvent = new RoomEvent(RoomEvent.ROOM_CREATE);
				event.roomName = name;
				dispatchEvent(event);
			}
		}

		public function showInfo(msg:String):void
		{
			infoLabel.text = msg;
		}

		override public function set isEnabled(value:Boolean):void
		{
			super.isEnabled = value;
			okButton.isEnabled = value;
			cancelButton.isEnabled = value;
			nameInput.isEnabled = value;

		}
	}
}
